<?php

namespace api\modules\v1\controllers\rbac;

use Yii;
use api\controllers\OnAuthController;
use api\modules\v1\forms\rbac\MenuForm;
use common\models\rbac\Menu;
use common\models\rbac\Module;
use common\models\member\Member;
use common\helpers\CommonHelper;

/**
 * Class MenuController
 * @package api\modules\v1\controllers\rbac
 */
class MenuController extends OnAuthController
{
    /**
     * @var Menu
     */
    public $modelClass = Menu::class;

    /**
     * @api {get} /v1/rbac/menu/list 菜单列表
     * @apiName list
     * @apiGroup Menu
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   部门列表
     * @apiSuccess {Number}  data.list.id                菜单ID
     * @apiSuccess {Number}  data.list.parent_menu_id    父ID
     * @apiSuccess {Number}  data.list.module_id         模块ID
     * @apiSuccess {String}  data.list.name              菜单名称
     * @apiSuccess {String}  data.list.pname             父菜单名称
     * @apiSuccess {String}  data.list.module_name       模块名称
     * @apiSuccess {String}  data.list.flag              标识
     * @apiSuccess {String}  data.list.url               菜单url
     * @apiSuccess {String}  data.list.class_style       菜单ico
     * @apiSuccess {Number}  data.list.sort              排序
     * @apiSuccess {Number}  data.list.status            状态
     * @apiSuccess {String}  data.list.status_str        状态值
     * @apiSuccess {String}  data.list.created_at_str    创建时间
     */
    public function actionList()
    {
        if (Yii::$app->user->identity->member->is_super != Member::IS_SUPER_YES) {
            return CommonHelper::api(422, '非超管，无权限访问');
        }

        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $data = Yii::$app->services->menu->getListPage($params, (int)$page, (int)$pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->menu->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/rbac/menu/info 菜单详情
     * @apiName info
     * @apiGroup Menu
     *
     * @apiDescription 新增ID不传，编辑必传ID
     *
     * @apiParam {Number} id 菜单ID【选填】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 菜单ID
     * @apiSuccess {Number} data.parent_menu_id     父菜单ID
     * @apiSuccess {Number} data.module_id          模块ID
     * @apiSuccess {String} data.name               菜单名称
     * @apiSuccess {String} data.url                菜单Url
     * @apiSuccess {String} data.class_style        菜单icon
     * @apiSuccess {Number} data.status             菜单状态
     * @apiSuccess {Number} data.sort               菜单排序
     * @apiSuccess {Array}  data.module_list        全部模块列表【循环】
     * @apiSuccess {Number} data.module_list.id     模块ID
     * @apiSuccess {String} data.module_list.name   模块名
     * @apiSuccess {Array}  data.menu_list          全部菜单列表【循环】
     * @apiSuccess {Number} data.menu_list.value    菜单ID
     * @apiSuccess {String} data.menu_list.title    菜单名
     * @apiSuccess {String} data.menu_list.checked  是否选中【true or false】
     * @apiSuccess {String} data.menu_list.expand   是否展开【true or false】
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        // 菜单信息
        $menu = Yii::$app->services->menu->findById($id);

        // 模块列表
        $moduleList = Yii::$app->services->module->findList([
            'select' => ['id', 'name'],
            'status' => Module::STATUS_ENABLE,
        ]);

        // 菜单列表
        $menuList = Yii::$app->services->menu->findList(['status' => Menu::STATUS_ENABLE]);

        // 菜单列表格式化
        $checkId = $menu ? $menu->parent_menu_id : 0;
        $menuListFormat = Yii::$app->services->menu->getListModuleFormat($menuList, 0, $checkId);

        return [
            'id' => $menu->id ?? 0,
            'parent_menu_id' => $menu->parent_menu_id ?? 0,
            'module_id' => $menu->module_id ?? 0,
            'name' => $menu->name ?? '',
            'url' => $menu->url ?? '',
            'class_style' => $menu->class_style ?? '',
            'status' => $menu->status ?? Menu::STATUS_ENABLE,
            'sort' => $menu->sort ?? 0,
            'module_list' => $moduleList,
            'menu_list' => $menuListFormat,
        ];
    }

    /**
     * @api {post} /v1/rbac/menu/create 新增菜单
     * @apiName create
     * @apiGroup Menu
     *
     * @apiParam {String} name              菜单名称
     * @apiParam {Number} module_id         模块ID
     * @apiParam {Number} parent_menu_id    父菜单ID【选填，顶级传0】
     * @apiParam {String} url               菜单url
     * @apiParam {String} class_style       菜单icon
     * @apiParam {Number} sort              排序【选填，不能小于0】
     * @apiParam {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
    public function actionCreateMenu()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['parent_menu_id'] = intval($params['parent_menu_id'] ?? 0);
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new MenuForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->menu->create($model);
    }

    /**
     * @api {post} /v1/rbac/menu/update 编辑菜单
     * @apiName update
     * @apiGroup Menu
     *
     * @apiParam {Number} id                菜单ID
     * @apiParam {String} name              菜单名称
     * @apiParam {Number} module_id         模块ID
     * @apiParam {Number} parent_menu_id    父菜单ID【选填，顶级传0】
     * @apiParam {String} url               菜单url
     * @apiParam {String} class_style       菜单icon
     * @apiParam {Number} sort              排序【选填，不能小于0】
     * @apiParam {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
    public function actionUpdateMenu()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['parent_menu_id'] = intval($params['parent_menu_id'] ?? 0);
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new MenuForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->menu->edit($model);
    }

    /**
     * @api {post} /v1/rbac/menu/delete 删除菜单
     * @apiName delete
     * @apiGroup Menu
     *
     * @apiParam {Number} id                菜单ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
    public function actionDeleteMenu()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;

        $model = new MenuForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->menu->delete($model);
    }

    /**
     * @api {post} /v1/rbac/menu/update-status 编辑菜单状态
     * @apiName update-status
     * @apiGroup Menu
     *
     * @apiParam {Number} id                菜单ID
     * @apiParam {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
    public function actionUpdateMenuStatus()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;

        $model = new MenuForm();
        $model->setScenario('edit-status');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->menu->editStatus($model);
    }
}
